package com.web.lang;

import java.lang.reflect.Field;

import com.app.AnnFieldPopulator;
import com.web.annotations.Copy;

public class LanguagePopulator extends AnnFieldPopulator<String, Copy> {

	private TMXResourceBundle tmxResource;

	public LanguagePopulator(LanguageConfigBean languageConfig) {
		super(Copy.class);

		String lang = languageConfig.getLanguage().toString();

		String languageFile = languageConfig.getLanguagePath();

		this.tmxResource = new TMXResourceBundle(languageFile, lang);
	}

	@Override
	protected String newFieldValue(Field field, Copy annotation) {

		String copyId = annotation.id();
		String copyValue = tmxResource.getString(copyId);

		if (copyValue != null) {

			return copyValue;
		} else
			throw new IllegalStateException(String.format("The copy id '%s' is not found", copyId));
	}
}